//
//  DadaViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "DadaViewController.h"

@interface DadaViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)NSInteger number;
@end

@implementation DadaViewController
- (void)dealloc
{
    [super dealloc];
    [_tableView release];
   
    
}



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        //tableView
        [self createTableView];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 150) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    self.number = 20;
    //注册cell
    [_tableView registerClass:[DadaCell class] forCellReuseIdentifier:@"cell"];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(lowData)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(dataHandeler)];
    
    if (_dataArr != 0) {
        [self.tableView.mj_footer beginRefreshing];
    }
    
    
    
    
    [self.view addSubview:_tableView];
    //管理内存
    [_tableView release];
}

#warning 下拉刷新
- (void)lowData
{
    
    [AFNModel GETWithHandler:@"http://c.3g.163.com/nc/article/list/T1444289532601/0-20.html" completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [DadaModel modelHandlerWithArray:result[@"T1444289532601"]];
        
   
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.mj_header endRefreshing];
    }); }];

}

#warning 上拉加载

- (void)dataHandeler
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1444289532601/%ld-20.html", self.number]  completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [YuleModel modelHandlerWithArray:result[@"T1444289532601"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
    self.number += 20;
}





- (UITableView *)setWithUrlStr:(NSString *)str
{//处理数据
    [AFNModel GETWithHandler:str completion:^(id result) {
       
        self.dataArr = [DadaModel modelHandlerWithArray:result[@"T1444289532601"]];
        [_tableView reloadData];
    }];
    
    return _tableView;
}

#pragma mark - tableView协议实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DadaModel *dada = _dataArr[indexPath.row];
    DadaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLabel.text = dada.title;
    cell.commentCountLabel.text = [dada.replyCount stringValue];
    [cell.imag sd_setImageWithURL:[NSURL URLWithString:dada.imgsrc]];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 200;
    
}

#warning  cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DadaModel *dada = _dataArr[indexPath.row];
    //代理人调用方法
    [self.delegate DadaValue:dada.url];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
